package com.buss.product.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.buss.core.dic.RecordStatus;
import com.buss.core.utils.AutoArrayList;

@Entity
@Table(name="base_goods")
@TableGenerator(name="goods_pk_sequence",initialValue=1,allocationSize=1,pkColumnName="seq_col",
		pkColumnValue="goods_seq",valueColumnName="seq_val",table="product_pk_sequence")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonIgnoreProperties(value={"props","details"})
@SuppressWarnings("unchecked")
public class Goods implements Serializable {

	private static final long serialVersionUID = -426128760659303439L;

	/**商品ID*/
	@Id
	@GeneratedValue(strategy=GenerationType.TABLE,generator="goods_pk_sequence")
	private Long id;
	
	/**商品编码*/
	@Column(nullable=false,length=20)
	private String code;
	
	/**商品名称*/
	@Column(nullable=false,length=200)
	private String name;
	
	/**拼音全码*/
	@Column(nullable=false,length=2000)
	private String pinyin;
	
	/**拼音简码*/
	@Column(nullable=false,length=200)
	private String pinyinHead;
	
	/**商品货号*/
	@Column(length=50)
	private String styleNo;
	
	/**所属分类ID*/
	@ManyToOne(cascade = CascadeType.REFRESH,fetch = FetchType.EAGER)
	@JoinColumn(name="categoryId",nullable=false)
	private GoodsCategory category;
	
	/**所属品牌*/	
	@ManyToOne(cascade = CascadeType.REFRESH,fetch = FetchType.EAGER)
	@JoinColumn(name="brandId",nullable=false)
	private Brand brand;
	
	/**分店、库房编码 该商品用于分店/库房编码，(存储方式：库房1，库房2，库房3)，0表示多有分店/库房都可以使用该分类*/
	private String branchId;
	
	/**库存单位*/
	@Column(length=50)
	private String sku;
	
	/**入库单位*/
	@Column(length=50)
	private String entryUnit;
	
	/**入库单位转换比例(为库存单位和入库单位的转换比例)*/
	@Column(precision=10,scale=4)
	private Float skuToEntryScale;
	
	/**出库单位*/
	@Column(length=50)
	private String exUnit;
	
	/**出库单位转换比例(为库存单位和出库单位的转换比例)*/
	@Column(precision=10,scale=4)
	private Float skuToExUnitScale;
	
	/**零售单价(主要作为查考用，真正的销售价格，是在入库时根据设置的核算方式产生的)*/
	@Column(precision=10,scale=4)
	private Float price;
	
	/**
	 * 商品属性信息列表
	 */
	
	@OneToMany(cascade= {CascadeType.REFRESH,CascadeType.REMOVE},fetch=FetchType.LAZY,mappedBy="goods")
	private List<GoodsProp> props = new AutoArrayList(GoodsProp.class);
	
	/**
	 * 将商品属性信息列表转为Map保存
	 */
	@SuppressWarnings("unused")
	@Transient
	private Map<String, String[]> propsDataMap = new HashMap<String, String[]>();
	
	/**商品明细信息*/
	@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy="goods")
	private List<GoodsDetail> details = new AutoArrayList(GoodsProp.class);;
	
	/**商品基本属性信息*/
	@Lob
	private String extProp;
	
	/**商品描述*/
	@Lob	
	private String intro;
	
	/**排序序号*/
	private Long sortOrder;
	
	/**是否固定价格,销售时，如果是true，则不可修改销售价格，商品销售价格=商品售价+属性附加价格。*/
	private Boolean isFixed;
	
	/**记录状态*/
	@Enumerated(value=EnumType.STRING)
	private RecordStatus status;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPinyin() {
		return pinyin;
	}

	public void setPinyin(String pinyin) {
		this.pinyin = pinyin;
	}

	public String getPinyinHead() {
		return pinyinHead;
	}

	public void setPinyinHead(String pinyinHead) {
		this.pinyinHead = pinyinHead;
	}

	public String getStyleNo() {
		return styleNo;
	}

	public void setStyleNo(String styleNo) {
		this.styleNo = styleNo;
	}

	public GoodsCategory getCategory() {
		return category;
	}

	public void setCategory(GoodsCategory category) {
		this.category = category;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public String getBranchId() {
		return branchId;
	}

	public void setBranchId(String branchId) {
		this.branchId = branchId;
	}

	public String getSku() {
		return sku;
	}

	public void setSku(String sku) {
		this.sku = sku;
	}

	public String getEntryUnit() {
		return entryUnit;
	}

	public void setEntryUnit(String entryUnit) {
		this.entryUnit = entryUnit;
	}

	public Float getSkuToEntryScale() {
		return skuToEntryScale;
	}

	public void setSkuToEntryScale(Float skuToEntryScale) {
		this.skuToEntryScale = skuToEntryScale;
	}

	public String getExUnit() {
		return exUnit;
	}

	public void setExUnit(String exUnit) {
		this.exUnit = exUnit;
	}

	public Float getSkuToExUnitScale() {
		return skuToExUnitScale;
	}

	public void setSkuToExUnitScale(Float skuToExUnitScale) {
		this.skuToExUnitScale = skuToExUnitScale;
	}
	
	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public List<GoodsProp> getProps() {
		return props;
	}

	public void setProps(List<GoodsProp> props) {
		if(props != null){
			this.props.clear();
		}
		this.props.addAll(props);
	}

	public List<GoodsDetail> getDetails() {
		return details;
	}

	public void setDetails(List<GoodsDetail> details) {
		if(details != null){
			this.details.clear();
		}
		this.details.addAll(details);
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public Long getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(Long sortOrder) {
		this.sortOrder = sortOrder;
	}

	public RecordStatus getStatus() {
		return status;
	}

	public void setStatus(RecordStatus status) {
		this.status = status;
	}

	public Boolean getIsFixed() {
		return isFixed;
	}

	public void setIsFixed(Boolean isFixed) {
		this.isFixed = isFixed;
	}

	public Map<String, String[]> getPropsDataMap() {
		Map<String, String[]> rsMap = new HashMap<String, String[]>();
		if(getProps() != null){
			for(GoodsProp tmpProp : getProps()){
				rsMap.put(tmpProp.getCateProp().getCode(), tmpProp.getPropValueArr());
			}
		}		
		return rsMap;
	}

	public void setPropsDataMap(Map<String, String[]> propsDataMap) {
		this.propsDataMap = propsDataMap;
	}

	public String getExtProp() {
		return extProp;
	}

	public void setExtProp(String extProp) {
		this.extProp = extProp;
	}

	
	
}
